import {
  getDictData,
  getDictDatas
} from '@/api/app'
import request from '@/utils/request'
export const publicEvents = {
  data () {
    return {
      loading: false,
      submitLoading: true,
      selectionData: [],
      tableData: [],
      pagination: {
        currentPage: 1
      }
    }
  },
  methods: {
    routerBack () {
      this.$router.go(-1)
    },
    emitSelection (val) {
      this.selectionData = val
    },
    currentChange (val) {
      this.pagination.currentPage = val
    },
    onSubmit () {
      this.submitLoading = true
      this.pagination.currentPage = null
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
      // this.$refs.form.resetfield()
      Object.assign(this[formName], this.$options.data()[formName])
    },
    openDialog (dialogObj, attrObj) {
      Object.keys(attrObj).forEach(element => {
        this[dialogObj][element] = attrObj[element]
      })
    },
    downloadFile (url, title) {
      request({
        url,
        method: 'post',
        responseType: 'blob',
        data: {
          is_excel: 1
        }
      }).then(res => {
        console.log(res)
        const fileName = title + '.xls'
        const blob = new Blob([res.data], {
          type: 'application/x-xls'
        })
        if (window.navigator.msSaveOrOpenBlob) {
          navigator.msSaveBlob(blob, fileName)
        } else {
          var link = document.createElement('a')
          link.href = window.URL.createObjectURL(blob)
          link.download = fileName
          link.click()
          window.URL.revokeObjectURL(link.href)
        }
      }).catch(err => {
        console.log(err)
      })
    }
  },
  watch: {
    'pagination.currentPage': {
      handler (newVal, oldVal) {
        if (oldVal !== null) {
          this.getAssignmentInfo()
        }
      }
    }
  }
}

export const getResourceEvents = {
  data () {
    return {
      resourceList: {
        unitList: [],
        samp_classify: []
      }
    }
  },
  methods: {
    getDictData (key) {
      getDictData(key).then(res => {
        this.resourceList[key] = res
        if (this.listTypeInfo) {
          this.listTypeInfo[key] = res.map(item => {
            return {
              label: item.value,
              value: item.id
            }
          })
        }
      })
    },
    getDictDatas (keys) {
      getDictDatas(keys).then(res => {
        for (const key in res) {
          this.resourceList[key] = res[key]
          if (this.listTypeInfo) {
            this.listTypeInfo[key] = res[key].map(item => {
              return {
                label: item.value,
                value: item.id
              }
            })
          }
        }
      })
    }
  }
}
